package com.zenithmind.version.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.version.pojo.dto.TagCreateDTO;
import com.zenithmind.version.pojo.dto.TagUpdateDTO;
import com.zenithmind.version.pojo.query.TagQueryDTO;
import com.zenithmind.version.pojo.vo.TagVO;
import com.zenithmind.version.service.TagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标签管理控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "标签管理", description = "标签管理相关接口")
@RestController
@RequestMapping("/api/v1/tags")
@RequiredArgsConstructor
public class TagController {

    private final TagService tagService;

    @Operation(summary = "创建标签", description = "创建新的标签")
    @PostMapping
    public Result<TagVO> createTag(@Valid @RequestBody TagCreateDTO createDTO) {
        TagVO tag = tagService.createTag(createDTO);
        return Result.success(tag);
    }

    @Operation(summary = "更新标签", description = "更新标签信息")
    @PutMapping("/{id}")
    public Result<Boolean> updateTag(
            @Parameter(description = "标签ID") @PathVariable String id,
            @Valid @RequestBody TagUpdateDTO updateDTO) {
        Boolean result = tagService.updateTag(id, updateDTO);
        return Result.success(result);
    }

    @Operation(summary = "删除标签", description = "删除指定标签")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteTag(@Parameter(description = "标签ID") @PathVariable String id) {
        Boolean result = tagService.deleteTag(id);
        return Result.success(result);
    }

    @Operation(summary = "批量删除标签", description = "批量删除标签")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDeleteTags(@RequestBody List<String> ids) {
        Boolean result = tagService.batchDeleteTags(ids);
        return Result.success(result);
    }

    @Operation(summary = "分页查询标签", description = "分页查询标签列表")
    @PostMapping("/page")
    public Result<IPage<TagVO>> getTagPage(@Valid @RequestBody TagQueryDTO queryDTO) {
        IPage<TagVO> page = tagService.getTagPage(queryDTO);
        return Result.success(page);
    }

    @Operation(summary = "获取标签详情", description = "获取标签详细信息")
    @GetMapping("/{id}")
    public Result<TagVO> getTagDetail(@Parameter(description = "标签ID") @PathVariable String id) {
        TagVO tag = tagService.getTagDetail(id);
        return Result.success(tag);
    }

    @Operation(summary = "获取仓库标签", description = "获取仓库的标签列表")
    @GetMapping("/repository/{repositoryId}")
    public Result<List<TagVO>> getRepositoryTags(
            @Parameter(description = "仓库ID") @PathVariable String repositoryId,
            @Parameter(description = "标签类型") @RequestParam(required = false) Integer tagType,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        List<TagVO> tags = tagService.getRepositoryTags(repositoryId, tagType, limit);
        return Result.success(tags);
    }

    @Operation(summary = "根据名称获取标签", description = "根据标签名称获取标签信息")
    @GetMapping("/repository/{repositoryId}/name/{tagName}")
    public Result<TagVO> getTagByName(
            @Parameter(description = "仓库ID") @PathVariable String repositoryId,
            @Parameter(description = "标签名称") @PathVariable String tagName) {
        TagVO tag = tagService.getTagByName(repositoryId, tagName);
        return Result.success(tag);
    }

    @Operation(summary = "获取版本标签", description = "获取版本标签列表")
    @GetMapping("/repository/{repositoryId}/versions")
    public Result<List<TagVO>> getVersionTags(
            @Parameter(description = "仓库ID") @PathVariable String repositoryId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<TagVO> tags = tagService.getVersionTags(repositoryId, limit);
        return Result.success(tags);
    }

    @Operation(summary = "获取发布标签", description = "获取发布标签列表")
    @GetMapping("/repository/{repositoryId}/releases")
    public Result<List<TagVO>> getReleaseTags(
            @Parameter(description = "仓库ID") @PathVariable String repositoryId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<TagVO> tags = tagService.getReleaseTags(repositoryId, limit);
        return Result.success(tags);
    }

    @Operation(summary = "获取最新版本标签", description = "获取最新版本标签")
    @GetMapping("/repository/{repositoryId}/latest")
    public Result<TagVO> getLatestVersionTag(@Parameter(description = "仓库ID") @PathVariable String repositoryId) {
        TagVO tag = tagService.getLatestVersionTag(repositoryId);
        return Result.success(tag);
    }

    @Operation(summary = "获取提交标签", description = "获取提交的标签列表")
    @GetMapping("/commit/{commitId}")
    public Result<List<TagVO>> getCommitTags(@Parameter(description = "提交ID") @PathVariable String commitId) {
        List<TagVO> tags = tagService.getCommitTags(commitId);
        return Result.success(tags);
    }

    @Operation(summary = "搜索标签", description = "根据关键词搜索标签")
    @GetMapping("/search")
    public Result<List<TagVO>> searchTags(
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "关键词") @RequestParam String keyword,
            @Parameter(description = "标签类型") @RequestParam(required = false) Integer tagType,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<TagVO> tags = tagService.searchTags(repositoryId, keyword, tagType, limit);
        return Result.success(tags);
    }

    @Operation(summary = "获取我的标签", description = "获取用户创建的标签")
    @GetMapping("/my")
    public Result<List<TagVO>> getUserTags(
            @Parameter(description = "仓库ID") @RequestParam(required = false) String repositoryId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        String userId = UserContext.getUserId();
        List<TagVO> tags = tagService.getUserTags(userId, repositoryId, limit);
        return Result.success(tags);
    }

    @Operation(summary = "比较标签", description = "比较两个标签的差异")
    @GetMapping("/compare")
    public Result<Object> compareTagDifferences(
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "源标签") @RequestParam String fromTag,
            @Parameter(description = "目标标签") @RequestParam String toTag) {
        Object differences = tagService.compareTagDifferences(repositoryId, fromTag, toTag);
        return Result.success(differences);
    }

    @Operation(summary = "获取标签间提交", description = "获取两个标签之间的提交列表")
    @GetMapping("/commits-between")
    public Result<List<Object>> getCommitsBetweenTags(
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "源标签") @RequestParam String fromTag,
            @Parameter(description = "目标标签") @RequestParam String toTag) {
        List<Object> commits = tagService.getCommitsBetweenTags(repositoryId, fromTag, toTag);
        return Result.success(commits);
    }

    @Operation(summary = "获取标签统计", description = "获取标签统计信息")
    @GetMapping("/repository/{repositoryId}/statistics")
    public Result<Object> getTagStatistics(@Parameter(description = "仓库ID") @PathVariable String repositoryId) {
        Object statistics = tagService.getTagStatistics(repositoryId);
        return Result.success(statistics);
    }

    @Operation(summary = "复制标签", description = "复制标签到其他提交")
    @PostMapping("/{id}/copy")
    public Result<TagVO> copyTagToCommit(
            @Parameter(description = "标签ID") @PathVariable String id,
            @Parameter(description = "目标提交ID") @RequestParam String targetCommitId,
            @Parameter(description = "新标签名称") @RequestParam String newTagName) {
        String userId = UserContext.getUserId();
        TagVO tag = tagService.copyTagToCommit(id, targetCommitId, newTagName, userId);
        return Result.success(tag);
    }

    @Operation(summary = "检查标签名称", description = "检查标签名称是否存在")
    @GetMapping("/check-name")
    public Result<Boolean> checkTagNameExists(
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "标签名称") @RequestParam String tagName,
            @Parameter(description = "排除的标签ID") @RequestParam(required = false) String excludeId) {
        Boolean exists = tagService.checkTagNameExists(repositoryId, tagName, excludeId);
        return Result.success(exists);
    }

    @Operation(summary = "创建版本标签", description = "创建版本标签")
    @PostMapping("/version")
    public Result<TagVO> createVersionTag(
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "提交ID") @RequestParam String commitId,
            @Parameter(description = "版本号") @RequestParam String version,
            @Parameter(description = "描述") @RequestParam(required = false) String description) {
        String userId = UserContext.getUserId();
        TagVO tag = tagService.createVersionTag(repositoryId, commitId, version, description, userId);
        return Result.success(tag);
    }

    @Operation(summary = "创建发布标签", description = "创建发布标签")
    @PostMapping("/release")
    public Result<TagVO> createReleaseTag(
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "提交ID") @RequestParam String commitId,
            @Parameter(description = "发布名称") @RequestParam String releaseName,
            @Parameter(description = "描述") @RequestParam(required = false) String description) {
        String userId = UserContext.getUserId();
        TagVO tag = tagService.createReleaseTag(repositoryId, commitId, releaseName, description, userId);
        return Result.success(tag);
    }

    @Operation(summary = "获取发布说明", description = "获取标签发布说明")
    @GetMapping("/{id}/release-notes")
    public Result<Object> getTagReleaseNotes(@Parameter(description = "标签ID") @PathVariable String id) {
        Object releaseNotes = tagService.getTagReleaseNotes(id);
        return Result.success(releaseNotes);
    }

    @Operation(summary = "更新发布说明", description = "更新标签发布说明")
    @PutMapping("/{id}/release-notes")
    public Result<Boolean> updateTagReleaseNotes(
            @Parameter(description = "标签ID") @PathVariable String id,
            @Parameter(description = "发布说明") @RequestBody String releaseNotes) {
        String userId = UserContext.getUserId();
        Boolean result = tagService.updateTagReleaseNotes(id, releaseNotes, userId);
        return Result.success(result);
    }
}
